-- SkyResourceM
-- created by panyl
-- 天空之城物资模块

module("SkyResourceM", package.seeall);

local resources = {};
local aliasResources = {};
local skyTreasure = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    resources = {};
    skyTreasure = {};

    -- 转换下sky_resource_CSV信息表
    local sky_resource_CSV = get_sky_resource_CSV();
    for i = 1, #sky_resource_CSV do
        local row = sky_resource_CSV[i];
        resources[row.class_id] = row;

        aliasResources[row.alias] = aliasResources[row.alias] or {};
        table.insert(aliasResources[row.alias], row);
    end

    -- 转换下sky_treasure_CSV信息表
    local sky_treasure_CSV = get_sky_treasure_CSV();
    for i = 1, #sky_treasure_CSV do
        local row = sky_treasure_CSV[i];
        skyTreasure[row.id] = row;
    end
end

-- 模块初始化操作
function init()
    loadCsv();

    EventMgr.removeAll("SkyResourceM");
end

-- 检索
function query(id, path)
    local m = resources[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 根据别名获取资源信息
function queryByAlias(alias, path)
    local m = aliasResources[alias];
    if not m then
        return;
    end

    if not path then
        return m[1];
    end

    return m[1][path];
end

-- 获取所有的资源的class_id
function getAllResources()
    return table.keys(resources);
end

-- 根据分页获取资源列表
function getResourcesByPage(pageName)
    local ret = {};

    local resourceIds = getAllResources();

    local resourceInfo = {};
    local info = {};
    for _, classId in pairs(resourceIds) do
        resourceInfo = query(classId);

        if resourceInfo["page"] == pageName then
            local item = table.deepcopy(resourceInfo);
            item.amount = ItemM.getAmount(ME.user, classId);
            item.icon = ItemM.query(classId, "icon")
            item.rank = ItemM.query(classId, "rank")
            if resourceInfo["alias"] == "captain_dairy" then
                -- 如果是日记，需要获得后才显示
                if item.amount > 0 then
                    table.insert(ret, item);
                end
            elseif item.amount > 0 or item.show == 1 then
                table.insert(ret, item);
            end
        end
    end    
    
    return ret;
end

-- 发现浮岛宝藏
function discoveryTreasure(index, treasureId)
    -- TODO
end

-- 检索宝藏配置
function querySkyTreasure(id, path)
    local m = skyTreasure[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 判断能否购买资源项
function canBuyResourceItem(classId, num)
    local sellPrice = ItemM.query(classId, "sell_price");
    local cost = sellPrice[1];

    if cost[1] == 1 then
        return ItemM.getAmount(ME.user, cost[2]) >= cost[3] * num;
    elseif cost[1] == 2 then
        return ME.user.dbase:query(cost[2], 0) >= cost[3] * num;
    end
end

-- 购买资源项
function buyResourceItem(classId, num)
    local sellPrice = ItemM.query(classId, "sell_price");
    local cost = sellPrice[1];

        -- 能否购买
    if not canBuyResourceItem(classId, num) then
        return false;
    end

    if cost[1] == 1 then
        if not ItemM.costAmount(ME.user, cost[2], cost[3] * num) then
            return false;
        end
    elseif cost[1] == 2 then
        if not ME.user:costAttrib(cost[2], cost[3] * num) then
            return false;
        end
    end

    -- 奖励
    BonusM.doBonus({1, classId, num}, "buy_resource_item");

    -- 抛出事件
    EventMgr.fire(event.BUY_SKY_RESOURCE_ITEM, { ["class_id"] = classId, ["amount"] = num });
    return true;
end